/*******************************************************************************
 * @filename c328r.h
 * Copyright (c) 2012 - UFAM Universidade Federal do Amazonas
 *
 * @brief Program for make communication between dsPIC and  camera C328R
 *
 * Originally created on 03/05/2012 by Igor Oliveira
 *------------------------------------------------------------------------------
 * $LastChangedBy:: Igor                                                   $
 * $LastChangedDate:: 2012-05-03 03:26:05 -0400 (Qui, 12 Abr 2012)         $
 * $Revision:: 05                                                          $
 *------------------------------------------------------------------------------
 */

#ifndef C328R_H_
#define C328R_H_

/**
 * Defines
 */
#define UCHAR unsigned char
UCHAR ACK  = 0x0E;
UCHAR NACK = 0x0F;

/**
 * General Structs
 */
typedef struct pattern{
    UCHAR ID;
    UCHAR Parameter1;
    UCHAR Parameter2;
    UCHAR Parameter3;
    UCHAR Parameter4;
    UCHAR Parameter5;
}Pattern;


/**
 * Globals variables/structs
 */
    Pattern sSendSync;
    Pattern sSendAck;
    Pattern sReceiveSync;
    Pattern sReceiveAck;
    Pattern sclr;
    Pattern snap_pic;
    Pattern ini_jpg;

/**
 * Standard result code type for all functions
 */
typedef enum{
        /** No error occurred */
        SUCCESS         =        0x00,
        /** generic non-specified error code */
        ERROR           =        0x01,
        /** Error: Wrong parameter range */
        ERROR_PARAM     =         0x02,
        /** Error: Callee is busy doing something, try later */
        BUSY            =         0x03
} error_t;

/**
 * Standard result type for all color types
 */
typedef enum ColorType{
      CT_GRAYSCALE_2 = 0x01,
      CT_GRAYSCALE_4 = 0x02,
      CT_GRAYSCALE_8 = 0x03,
      CT_COLOR_12 = 0x05,
      CT_COLOR_16 = 0x06,
      CT_JPEG = 0x07
};

/**
 * Standard result type for all resolutions
 */
typedef enum PreviewResolution{
      PR_80x60 = 0x01,
      PR_160x120 = 0x03
};

/**
 * Standard result type for all types of resolution JPEG
 */
typedef enum JPEGResolution{
      JR_80x64 = 0x01,
      JR_160x128 = 0x03,
      JR_320x240 = 0x05,
      JR_640x480 = 0x07
};

/**
 * Standard result type for the types compressed or not compressed
 */
typedef enum SnapshotType{
      ST_COMPRESSED = 0x00,
      ST_UNCOMPRESSED = 0x01
};

/**
 * Standard result type for all types of pictures
 */
typedef enum PictureType{
      PT_SNAPSHOT = 0x01,
      PT_PREVIEW = 0x02,
      PT_JPEG = 0x05
};


/*************************************
 * @brief Prototype of the functions *
 *************************************/
 
/**
 * Send command SYNC, and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t send_cmd();

/**
 * Send command send_IniJpg, and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t InitJpg();
  
/**
 * Send command send_PackSize and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t set_package_size();

/**
 * Send command send_SnapCompressedPict and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t snapshot_compressed_picture();
 
/**
 * Send command send_GetPicSnap and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t get_picture_snapshot();
 
/**
 * Send command send_BaudRate and wait for receive ACK
 * @param none
 * @return SUCCESS
 */
  error_t SetBaudRate();

/**
 * Call the functions and realize test with the returns these functions
 * @param none
 * @return none
 */
  error_t init_config();
  
/**
 * Initial protocol communication with camera
 * @param none
 * @return none
 */
  void InitialProtocol();
  
/**
 * Starts to receive data from the camera
 * @param none
 * @return SUCCESS
 */
  error_t DataReceive();

/**
 * Clear the struct
 * @param none
 * @return UCHAR
 */
  Pattern clear(struct pattern *clr);
  

/******************************************
 * @brief Commands of the synchronization *
 ******************************************/

/**
 * set the configure UART
 * @param none
 * @return none
 */
   void uart_config(void);

/**
 * Send command Sync AA 0D 00 00 00 00 to camera
 * @param none
 * @return none
 */
  void send_sync(void);

/**
 * Send command Ack AA 0E 0D 00 00 00 to camera
 * @param none
 * @return none
 */
  void send_ack(void);

/**
 * Receive ACK signals of the camera and store in the struct receive_ack
 * @param pointer to struct sReceiveAck
 * @return pointer to struct sReceiveAck
 */
 Pattern receive_ack(struct pattern val);

/**
 * Receive SYNC signals of the camera and store in the struct receive_sync
 * @param pointer to struct sReceiveSync
 * @return pointer to struct sReceiveSync
 */
  Pattern receive_sync(struct pattern *);
  
/**
 * Receive Data of the camera AA 0A 01 ~~ ~~ ~~
 * @param pointer to struct snap_pic
 * @return pointer to struct snap_pic
 */
  Pattern receive_SnapPict(struct pattern *);
  
/**
 * Send command AA 09 00 00 00 00 (shutdown/reboot camera)
 * @param none
 * @return none
 */
  error_t PowerOff();
  

  
/****************************************
 * @brief  Function for send to camera  *
 ****************************************/

/**
 * Send the command of the size
 * Send command AA 06 08 00 02 00 (size package)
 * @param none
 * @return none
 */
  void send_PackSize();

/**
 * Send command AA 04 01 00 00 00 (get JPEG)
 * @param none
 * @return none
 */
  void send_GetPicSnap();

/**
 * Send command to configure baud rates
 * Send command AA 07 7F 01 00 00 (set baud rate)
 * @param none
 * @return none
 */
  void send_BaudRate();

/**
 * Send the command to get the picture Snapshot
 * Send command AA 05 00 00 00 00 (get JPEG)
 * @param none
 * @return none
 */
  void send_SnapCompressedPict();

/**
 * Send command to Power Off the camera
 * Send command AA 09 00 00 00 00 (shut down)
 * @param none
 * @return none
 */
  void send_PowerOff();
  
/**
 * Send command AA 08 00 00 00 FF (reboot camera)
 * @param none
 * @return none
 */
  void send_Reset();

/**
 * Send command AA 01 00 07 yy 07 to get JPEG
 * @param none
 * @return none
 */  
  void send_IniJpg(void);

/**
 * Send command AA 0E xx xx xx xx to begin receiving data
 * @param none
 * @return none
 */
  void send_ackPackageID(void);


/************************
 * @brief Test functios *
 ************************/

/**
 * Function to test other functions
 * @param pointer to pattern struct 
 * @return values of parameter of the struct
 */   
  void test(struct pattern *);

/**
 * Function to test other functions
 * @param none
 * @return 00 00 00 00 00 00
 */
  void test1();


#endif /*C328R_H_*/